﻿using Gp.Scripts.Core;
using Gp.Scripts.Data.Skills;
using QFramework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.GUI {
    public class SkillExecutionZone : MonoBehaviour, IController 
    {
        [SerializeField] private Image icon;
        [SerializeField] private TMP_Text title;
        [SerializeField] private TMP_Text description;
        
        
        [SerializeField] private SkillModeSelectionButton prefab;
        [SerializeField] private Transform modeSelectionButton;
        // [SerializeField] private MagicMergeUIZone magicMergeUIZone;
        public const int MaxButtonCount = 6;


        private SkillModeSelectionButton[] _buttons;
        public SkillEntity CurSkill { get; set; }
        private Transform mCancelButton;
        
        private void Start() {
            _buttons = new SkillModeSelectionButton[MaxButtonCount];
            mCancelButton = transform.Find("Image/BtnCancel");
            mCancelButton.GetComponent<Button>().onClick.AddListener(Global.SendEvent<Event_SkillUI.Cancel>);

            for (int i = 0; i < _buttons.Length; i++) {
                var obj = Instantiate(prefab, modeSelectionButton);
                _buttons[i] = obj;
                
                obj.OnInit(i, this);
            }
        }


        public void OnShow(SkillEntity skill) {
            var info = skill.RenderInfo();
            CurSkill = skill;

            icon.sprite = info.Icon;
            title.text = info.NameText;
            description.text = info.DescriptionText;


            // if (skill is IMagicMergeSkill mms) {
            //     magicMergeUIZone.Show(mms);
            //     return;
            // }

            if (skill is not IModeSkill ms) {
                return;
            }

            for (int i = 0; i < _buttons.Length; i++) {
                if (i >= ms.GetInfos().Length) {
                    _buttons[i].Hide();
                    continue;
                }
                _buttons[i].Render(ms.GetInfos()[i]);
            }
            
            
            MarkSelection(ms.CurMode, true);
        }


        public void ClearUp() {
            icon.sprite = null;
            title.text = "";
            description.text = "";
            CurSkill = null;
            
            // magicMergeUIZone.Hide();
            foreach (var button in _buttons) {
                button.Hide();
            }
        }

        
        public void MarkSelection(int index, bool firstMark = false) {
            // if (CurSkill is not IModeSkill ms) return;
            //
            // foreach (var button in _buttons) {
            //     button.UnMarkSelecting();
            // }
            //
            // ms.CurMode = index;
            // _buttons[index].MarkSelecting();
            // if (!firstMark) Global.Get<BattleSystem>().SkillSelected(CurSkill);
        }
        
        
        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}